package com.mit.web.business;

import java.util.List;

import com.mit.common.RequestContext;
import com.mit.common.ResultContext;
import com.mit.common.design.dto.FabricDTO;
import com.mit.common.design.dto.StyleDTO;
import com.mit.common.design.dto.TemplateDTO;
import com.mit.common.design.dto.TemplatePartDTO;
import com.mit.common.exception.BaseException;
import com.mit.common.exception.NoRecordUpdateException;
import com.mit.common.security.dto.UserDTO;
import com.mit.common.token.DesignTokens;
import com.mit.common.token.Tokens;

public class DesignService extends RemoteService {

	private final String FACADE_NAME = "DesignFacade";
	@Override
	public String getFacadeName() {
		return FACADE_NAME;
	}
	
	public StyleDTO updateStyle(StyleDTO style, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("saveStyle", currentUser);
		rqs.addParameter(DesignTokens.KEY_STYLE_DTO, style);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);	
		style = (StyleDTO) rlt.getResult(DesignTokens.KEY_STYLE_DTO);
		return style;
	}
	
	public List<StyleDTO> retrievePagiedStyles(String searchCode, String searchState, 
			Integer pageNo, Integer pageSize, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrievePagedStyles", currentUser);
		rqs.addParameter(DesignTokens.KEY_STYLE_CODE, searchCode);
		rqs.addParameter(DesignTokens.KEY_STYLE_STATE, searchState);
		rqs.addParameter(Tokens.KEY_PAGE_NO, pageNo);
		rqs.addParameter(Tokens.KEY_PAGE_SIZE, pageSize);

		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		List<StyleDTO> lst = (List<StyleDTO>) rlt.getResult(DesignTokens.KEY_STYLE_LIST);
		return lst;
	}
	
	public Integer retrieveStylesCount(String searchCode, String searchState, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrieveStyleRecordCount", currentUser);
		rqs.addParameter(DesignTokens.KEY_STYLE_CODE, searchCode);
		rqs.addParameter(DesignTokens.KEY_STYLE_STATE, searchState);
		
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);	
		Integer rc = (Integer) rlt.getResult(Tokens.KEY_RECORD_COUNT);
		return rc;
	}
	
	public StyleDTO retrieveStyleById(Integer id, UserDTO currentUser) throws BaseException {
		RequestContext rqs = createRequestContext("retrieveStyleById", currentUser);
		rqs.addParameter(DesignTokens.KEY_STYLE_ID, id);
		
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);	
		StyleDTO to = (StyleDTO) rlt.getResult(DesignTokens.KEY_STYLE_DTO);
		return to;
	}
	
	public List<TemplateDTO> retrievePagiedTemplates(String searchCode, String searchState, 
			Integer pageNo, Integer pageSize, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrievePagedTemplates", currentUser);
		rqs.addParameter(DesignTokens.KEY_STYLE_CODE, searchCode);
		rqs.addParameter(DesignTokens.KEY_TEMP_STATE, searchState);
		rqs.addParameter(Tokens.KEY_PAGE_NO, pageNo);
		rqs.addParameter(Tokens.KEY_PAGE_SIZE, pageSize);

		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		List<TemplateDTO> lst = (List<TemplateDTO>) rlt.getResult(DesignTokens.KEY_TEMP_LIST);
		return lst;
	}
	
	public Integer retrieveTemplatesCount(String searchCode, String searchState, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrieveTemplatesRecordCount", currentUser);
		rqs.addParameter(DesignTokens.KEY_STYLE_CODE, searchCode);
		rqs.addParameter(DesignTokens.KEY_TEMP_STATE, searchState);
		
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);	
		Integer rc = (Integer) rlt.getResult(Tokens.KEY_RECORD_COUNT);
		return rc;
	}
	
	public List<TemplatePartDTO> retrieveTemplateDetails(Integer templateId, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrieveTemplateDetails", currentUser);
		rqs.addParameter(DesignTokens.KEY_TEMP_ID, templateId);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);	
		List<TemplatePartDTO> list = (List<TemplatePartDTO>) rlt.getResult(DesignTokens.KEY_TEMP_DTL_LST);
		return list;
	}
	
	public TemplateDTO updateTemplate(TemplateDTO to, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("saveTemplate", currentUser);
		rqs.addParameter(DesignTokens.KEY_TEMP_DTO, to);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);
		return (TemplateDTO) rlt.getResult(DesignTokens.KEY_TEMP_DTO);
	}
	
	public List<TemplateDTO> retrieveTemplatesByStyle(Integer styleId, String state, Integer excludedId, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrieveTemplatesByStyleId", currentUser);
		rqs.addParameter(DesignTokens.KEY_STYLE_ID, styleId);
		rqs.addParameter(DesignTokens.KEY_TEMP_ID, excludedId);
		rqs.addParameter(DesignTokens.KEY_TEMP_STATE, state);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);	
		List<TemplateDTO> list = (List<TemplateDTO>)  rlt.getResult(DesignTokens.KEY_TEMP_LIST);
		return list;
	}
	
	
	public List<StyleDTO> retrievePagedSampleTasks(String searchCode, String taskState, 
			Integer pageNo, Integer pageSize, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrievePagedSampleTasks", currentUser);
		rqs.addParameter(DesignTokens.KEY_STYLE_CODE, searchCode);
		rqs.addParameter(DesignTokens.KEY_SAMPLE_STATE, taskState);
		rqs.addParameter(Tokens.KEY_PAGE_NO, pageNo);
		rqs.addParameter(Tokens.KEY_PAGE_SIZE, pageSize);

		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		List<StyleDTO> lst = (List<StyleDTO>) rlt.getResult(DesignTokens.KEY_STYLE_LIST);
		return lst;
	}
	
	public Integer retrieveSampleTaskCount(String searchCode, String taskState, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrieveSampleTaskCount", currentUser);
		rqs.addParameter(DesignTokens.KEY_STYLE_CODE, searchCode);
		rqs.addParameter(DesignTokens.KEY_SAMPLE_STATE, taskState);
		
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);	
		Integer rc = (Integer) rlt.getResult(Tokens.KEY_RECORD_COUNT);
		return rc;
	}
	

	public StyleDTO saveSampleTask(StyleDTO style, UserDTO currentUser) throws BaseException {
		RequestContext rqs = createRequestContext("saveSampleTask", currentUser);
		rqs.addParameter(DesignTokens.KEY_STYLE_DTO, style);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);	
		return (StyleDTO) rlt.getResult(DesignTokens.KEY_STYLE_DTO);
	}


	public StyleDTO approveSample(StyleDTO style, UserDTO currentUser) throws BaseException {
		RequestContext rqs = createRequestContext("approveSample", currentUser);
		rqs.addParameter(DesignTokens.KEY_STYLE_DTO, style);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);	
		return (StyleDTO) rlt.getResult(DesignTokens.KEY_STYLE_DTO);
	}
	
	public List<StyleDTO> retrievePagedSpecTasks(String searchCode, String taskState, 
			Integer pageNo, Integer pageSize, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrievePagedSpecTasks", currentUser);
		rqs.addParameter(DesignTokens.KEY_STYLE_CODE, searchCode);
		rqs.addParameter(DesignTokens.KEY_SAMPLE_STATE, taskState);
		rqs.addParameter(Tokens.KEY_PAGE_NO, pageNo);
		rqs.addParameter(Tokens.KEY_PAGE_SIZE, pageSize);

		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		List<StyleDTO> lst = (List<StyleDTO>) rlt.getResult(DesignTokens.KEY_STYLE_LIST);
		return lst;
	}
	
	public Integer retrieveSpecTaskCount(String searchCode, String taskState, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrieveSpecTaskCount", currentUser);
		rqs.addParameter(DesignTokens.KEY_STYLE_CODE, searchCode);
		rqs.addParameter(DesignTokens.KEY_SAMPLE_STATE, taskState);
		
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);	
		Integer rc = (Integer) rlt.getResult(Tokens.KEY_RECORD_COUNT);
		return rc;
	}
	

	public StyleDTO saveSpecTask(StyleDTO style, UserDTO currentUser) throws BaseException {
		RequestContext rqs = createRequestContext("saveSpecTask", currentUser);
		rqs.addParameter(DesignTokens.KEY_STYLE_DTO, style);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);	
		return (StyleDTO) rlt.getResult(DesignTokens.KEY_STYLE_DTO);
	}	
	
	public StyleDTO approveStyle(Integer id, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("approveStyle", currentUser);
		rqs.addParameter(DesignTokens.KEY_STYLE_ID, id);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);	
		return (StyleDTO) rlt.getResult(DesignTokens.KEY_STYLE_DTO);
	}
	public StyleDTO terminateStyle(Integer id, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("terminateStyle", currentUser);
		rqs.addParameter(DesignTokens.KEY_STYLE_ID, id);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);	
		return (StyleDTO) rlt.getResult(DesignTokens.KEY_STYLE_DTO);
	}
	public StyleDTO deleteStyle(Integer id, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("deleteStyle", currentUser);
		rqs.addParameter(DesignTokens.KEY_STYLE_ID, id);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);	
		return (StyleDTO) rlt.getResult(DesignTokens.KEY_STYLE_DTO);
	}
	
	public List<StyleDTO> retrieveApprovedStyles(Integer year, Integer season, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrieveApprovedStyles", currentUser);
		rqs.addParameter(DesignTokens.KEY_STYLE_YEAR, year);
		rqs.addParameter(DesignTokens.KEY_STYLE_SEASON	, season);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);	
		return (List<StyleDTO>) rlt.getResult(DesignTokens.KEY_STYLE_LIST);
	}
}
